package org.yzz.auth.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.yzz.auth.domain.Role;
import org.yzz.auth.dto.MenuDTO;
import org.yzz.auth.dto.RoleDTO;
import org.yzz.auth.mapper.MenuMapper;
import org.yzz.auth.mapper.RoleMapper;
import org.yzz.auth.query.RoleQuery;
import org.yzz.auth.service.IRoleService;
import org.yzz.base.service.impl.BaseServiceImpl;
import org.yzz.base.utils.PageInfo;

import java.util.List;
import java.util.Objects;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper mapper;


    @Override
    @Transactional
    public void insertByRoleId(RoleDTO roleDTO) {
        // 先删除该角色的权限
        mapper.deleteByRoleId(roleDTO.getId());
        // 保存权限
        mapper.insertByRoleId(roleDTO);
    }

    @Override
    public List<String> selectByRoleId(Long id) {
        return mapper.selectByRoleId(id);
    }

    @Override
    public List<Long> selectAllId() {
        return mapper.selectAllId();
    }

    @Override
    @Transactional
    public void insertByMenuId(MenuDTO menuDTO) {
        // 先删除该角色的菜单
        mapper.deleteByMenuId(menuDTO.getId());
        if(Objects.isNull(menuDTO.getMenuIds()) || menuDTO.getMenuIds().size()==0) return;
        // 保存菜单
        mapper.insertByMenuId(menuDTO);
    }

    @Override
    public List<Long> selectMenusById(Long id) {
        return mapper.selectMenusById(id);
    }
}
